package com.steveproject.parttimeworktree.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.steveproject.parttimeworktree.App;
import com.steveproject.parttimeworktree.R;
import com.steveproject.parttimeworktree.entity.PartBaseEntity;
import com.steveproject.parttimeworktree.net.MyCallback;
import com.steveproject.parttimeworktree.net.Task;
import com.steveproject.parttimeworktree.ui.activity.PartLoginActivity;
import com.steveproject.parttimeworktree.utils.AppConfig;
import com.steveproject.parttimeworktree.utils.DataCleanManager;
import com.steveproject.parttimeworktree.utils.SPUtils;
import com.steveproject.parttimeworktree.utils.UserUtils;

import java.util.Map;
import java.util.Objects;
import java.util.Set;

import retrofit2.Response;

/**
 * 中间dialog的弹出
 */
public class CenterDialog {
    private Activity activity;
    private AlertDialog.Builder builder;
    private View v;

    public CenterDialog(int layoutId, Activity activity) {
        this.activity = activity;
        this.builder = new AlertDialog.Builder(activity);
        this.v = (LayoutInflater.from(activity)).inflate(layoutId, null);
    }

    /**
     * @param title 标题
     *              简单提示框  格式：内容、取消、确定
     */
    public void showPrompt(String title) {
        TextView content = v.findViewById(R.id.prompt_content);
        content.setText(title);
        TextView btn_sure = v.findViewById(R.id.userConfirm);
        TextView btn_cancel = v.findViewById(R.id.userCancel);
        final Dialog dialog = builder.create();
        Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawableResource(R.color.transparent);
        dialog.show();
        //此处设置位置窗体大小
        dialog.getWindow().setLayout(dip2px(activity, 280), LinearLayout.LayoutParams.WRAP_CONTENT);
        Objects.requireNonNull(dialog.getWindow()).setContentView(v);//自定义布局应该在这里添加，要在dialog.show()的后面
        dialog.getWindow().setGravity(Gravity.CENTER);//可以设置显示的位置
        dialog.setCancelable(false);
        btn_sure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Task.getApiService().partLogOut().enqueue(new MyCallback<PartBaseEntity>(activity) {
                    @Override
                    protected void onSuccess(Response<PartBaseEntity> response) {
                        UserUtils.clearUserInfo();
                        activity.startActivity(new Intent(activity, PartLoginActivity.class));
                        activity.finish();

                    }

                    @Override
                    protected void onFailure(Throwable t) {

                    }
                });
            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
    }


    private static int dip2px(Context mContext, float dpValue) {
        final float scale = mContext.getResources()
                .getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    //加载动画
    public Dialog showLoad(String str) {
        ImageView iv_load = v.findViewById(R.id.iv_load);//
        TextView tv_pt = v.findViewById(R.id.tv_pt);
        tv_pt.setText(str);
        Glide.with(activity).load(R.drawable.refresh_juhua)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(iv_load);
        final Dialog dialog = builder.create();
        Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawableResource(R.color.transparent);
        dialog.show();
        //此处设置位置窗体大小
        dialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        Objects.requireNonNull(dialog.getWindow()).setContentView(v);//自定义布局应该在这里添加，要在dialog.show()的后面
        dialog.getWindow().setGravity(Gravity.CENTER);//可以设置显示的位置
        dialog.setCancelable(false);
        return dialog;
    }


    /**
     * @param title 标题 退出应用
     *              简单提示框  格式：内容、取消、确定
     */
    public void showQuitPrompt(String title) {
        TextView content = v.findViewById(R.id.prompt_content);
        content.setText(title);
        TextView btn_sure = v.findViewById(R.id.userConfirm);
        TextView btn_cancel = v.findViewById(R.id.userCancel);
        final Dialog dialog = builder.create();
        Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawableResource(R.color.transparent);
        dialog.show();
        //此处设置位置窗体大小
        dialog.getWindow().setLayout(dip2px(activity, 280), LinearLayout.LayoutParams.WRAP_CONTENT);
        Objects.requireNonNull(dialog.getWindow()).setContentView(v);//自定义布局应该在这里添加，要在dialog.show()的后面
        dialog.getWindow().setGravity(Gravity.CENTER);//可以设置显示的位置
        dialog.setCancelable(false);
        btn_sure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                activity.finish();
            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
    }

    /**
     * 清除缓存
     *
     * @param title 标题
     *              简单提示框  格式：内容、取消、确定
     */

    public void clearMemory(String title, TextView tv) {
        TextView content = v.findViewById(R.id.prompt_content);
        content.setText(title);
        TextView btn_sure = v.findViewById(R.id.userConfirm);
        TextView btn_cancel = v.findViewById(R.id.userCancel);
        final Dialog dialog = builder.create();
        Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawableResource(R.color.transparent);
        dialog.show();
        //此处设置位置窗体大小
        dialog.getWindow().setLayout(dip2px(activity, 280), LinearLayout.LayoutParams.WRAP_CONTENT);
        Objects.requireNonNull(dialog.getWindow()).setContentView(v);//自定义布局应该在这里添加，要在dialog.show()的后面
        dialog.getWindow().setGravity(Gravity.CENTER);//可以设置显示的位置
        dialog.setCancelable(false);
        btn_sure.setOnClickListener(new View.OnClickListener() {

            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Map<String, ?> map = SPUtils.getAll(activity);
                String img_home_card1 = AppConfig.getInstance(App.getContext()).get("img_home_card1");
                String img_home_card2 = AppConfig.getInstance(App.getContext()).get("img_home_card2");
                String img_person_banner = AppConfig.getInstance(App.getContext()).get("img_person_banner");
                String icon_featured_card1 = AppConfig.getInstance(App.getContext()).get("icon_featured_card1");
                String icon_featured_card2 = AppConfig.getInstance(App.getContext()).get("icon_featured_card2");
                String icon_featured_card3 = AppConfig.getInstance(App.getContext()).get("icon_featured_card3");
                String icon_featured_card4 = AppConfig.getInstance(App.getContext()).get("icon_featured_card4");
                String img_home_card2_2 = AppConfig.getInstance(App.getContext()).get("img_home_card2_2");
                String img_home_card1_1 = AppConfig.getInstance(App.getContext()).get("img_home_card1_1");
                String job_detail = AppConfig.getInstance(App.getContext()).get("job_detail");
                String company_detail = AppConfig.getInstance(App.getContext()).get("company_detail");
                String user_agreement = AppConfig.getInstance(App.getContext()).get("user_agreement");
                String urivacy_protocol = AppConfig.getInstance(App.getContext()).get("urivacy_protocol");
                String pesume_preview = AppConfig.getInstance(App.getContext()).get("pesume_preview");
                String business_coop = AppConfig.getInstance(App.getContext()).get("business_coop");
                DataCleanManager.clearAllCache(App.getContext());//清除缓存
                tv.setText("0M");
                /*删除完之后 进行保存sharePreferences */
                for (int i = 0; i < map.size(); i++) {
                    Set<? extends Map.Entry<String, ?>> set = map.entrySet();
                    for (Map.Entry<String, ?> entry : set) {
                        SPUtils.put(activity, entry.getKey(), entry.getValue());
                    }
                }
                //还得保存所有的配置信息
                AppConfig.getInstance(App.getContext()).set("img_home_card1", img_home_card1);
                AppConfig.getInstance(App.getContext()).set("img_home_card2", img_home_card2);
                AppConfig.getInstance(App.getContext()).set("img_person_banner", img_person_banner);//个人中心banner
                AppConfig.getInstance(App.getContext()).set("icon_featured_card1", icon_featured_card1);
                AppConfig.getInstance(App.getContext()).set("icon_featured_card2", icon_featured_card2);
                AppConfig.getInstance(App.getContext()).set("icon_featured_card3", icon_featured_card3);
                AppConfig.getInstance(App.getContext()).set("icon_featured_card4", icon_featured_card4);
                AppConfig.getInstance(App.getContext()).set("img_home_card2_2", img_home_card2_2);
                AppConfig.getInstance(App.getContext()).set("img_home_card1_1", img_home_card1_1);
                AppConfig.getInstance(App.getContext()).set("job_detail", job_detail);
                AppConfig.getInstance(App.getContext()).set("company_detail", company_detail);
                AppConfig.getInstance(App.getContext()).set("user_agreement", user_agreement);
                AppConfig.getInstance(App.getContext()).set("urivacy_protocol", urivacy_protocol);
                AppConfig.getInstance(App.getContext()).set("pesume_preview", pesume_preview);
                AppConfig.getInstance(App.getContext()).set("business_coop", business_coop);

            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
    }
}
